//
//  WKNodeSelectCell.m
//  WuKongBase
//
//  Created by wangfei on 2025/9/21.
//

#import "WKNodeSelectCell.h"
#import "WuKongBase/WuKongBase.h"
#import "Masonry/Masonry.h"

@interface WKNodeSelectCell ()
@property (nonatomic, strong) UIView *cardView;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UILabel *detailLabel;
@property (nonatomic, strong) UILabel *currentLab;
@property (nonatomic, strong) UIView *speedDotView;
@property (nonatomic, strong) UIImageView *selectedIndicator;
@end

@implementation WKNodeSelectCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self setupSubviews];
    }
    return self;
}

- (void)setupSubviews {
    // 卡片视图
    self.cardView = [[UIView alloc] init];
    self.cardView.backgroundColor = [UIColor whiteColor];
    self.cardView.layer.cornerRadius = 8;
    self.cardView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.cardView.layer.shadowOpacity = 0.05;
    self.cardView.layer.shadowRadius = 4;
    self.cardView.layer.shadowOffset = CGSizeMake(0, 2);
    [self.contentView addSubview:self.cardView];
    
    // 节点名称
    self.nameLabel = [[UILabel alloc] init];
    self.nameLabel.font = [UIFont boldSystemFontOfSize:16];
    self.nameLabel.textColor = [WKApp shared].config.defaultTextColor;
    [self.cardView addSubview:self.nameLabel];
    
    // 详细信息（延迟 + 线路）
    self.detailLabel = [[UILabel alloc] init];
    self.detailLabel.font = [UIFont systemFontOfSize:14];
    self.detailLabel.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
    [self.cardView addSubview:self.detailLabel];
    
    self.currentLab = [[UILabel alloc] init];
    self.currentLab.font = [UIFont systemFontOfSize:14];
    self.currentLab.textColor = [UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:1.0];
    [self.cardView addSubview:self.currentLab];
    
    // 速度圆点
    self.speedDotView = [[UIView alloc] init];
    self.speedDotView.layer.cornerRadius = 6;
    [self.cardView addSubview:self.speedDotView];
    
    // 选中指示器
    self.selectedIndicator = [[UIImageView alloc] init];
    self.selectedIndicator.image = [UIImage imageNamed:@"checkmark_icon"];
    self.selectedIndicator.hidden = YES;
    [self.cardView addSubview:self.selectedIndicator];
    
    // 设置约束
    [self setupConstraints];
    
    self.cardView.backgroundColor = [WKApp shared].config.cellBackgroundColor;
}

- (void)setupConstraints {
    self.cardView.translatesAutoresizingMaskIntoConstraints = NO;
    self.nameLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.detailLabel.translatesAutoresizingMaskIntoConstraints = NO;
    self.speedDotView.translatesAutoresizingMaskIntoConstraints = NO;
    self.selectedIndicator.translatesAutoresizingMaskIntoConstraints = NO;
    
    [NSLayoutConstraint activateConstraints:@[
        // 卡片视图约束
        [self.cardView.topAnchor constraintEqualToAnchor:self.contentView.topAnchor constant:8],
        [self.cardView.leadingAnchor constraintEqualToAnchor:self.contentView.leadingAnchor constant:16],
        [self.cardView.trailingAnchor constraintEqualToAnchor:self.contentView.trailingAnchor constant:-16],
        [self.cardView.bottomAnchor constraintEqualToAnchor:self.contentView.bottomAnchor constant:-8],
        
        // 速度圆点约束
        [self.speedDotView.widthAnchor constraintEqualToConstant:12],
        [self.speedDotView.heightAnchor constraintEqualToConstant:12],
        [self.speedDotView.centerYAnchor constraintEqualToAnchor:self.nameLabel.centerYAnchor],
        [self.speedDotView.leadingAnchor constraintEqualToAnchor:self.cardView.leadingAnchor constant:16],
        
        // 节点名称约束
        [self.nameLabel.leadingAnchor constraintEqualToAnchor:self.speedDotView.trailingAnchor constant:12],
        [self.nameLabel.topAnchor constraintEqualToAnchor:self.cardView.topAnchor constant:16],
        
        // 详细信息约束
        [self.detailLabel.leadingAnchor constraintEqualToAnchor:self.nameLabel.leadingAnchor],
        [self.detailLabel.topAnchor constraintEqualToAnchor:self.nameLabel.bottomAnchor constant:8],
        [self.detailLabel.bottomAnchor constraintEqualToAnchor:self.cardView.bottomAnchor constant:-16],
        
        // 选中指示器约束
        [self.selectedIndicator.widthAnchor constraintEqualToConstant:24],
        [self.selectedIndicator.heightAnchor constraintEqualToConstant:24],
        [self.selectedIndicator.centerYAnchor constraintEqualToAnchor:self.cardView.centerYAnchor],
        [self.selectedIndicator.trailingAnchor constraintEqualToAnchor:self.cardView.trailingAnchor constant:-16]
    ]];
    
    [self.currentLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(0);
        make.right.mas_equalTo(-16);
    }];
}

- (void)configureWithNode:(WKServerNode *)node {
    self.nameLabel.text = node.name;
    self.detailLabel.text = [NSString stringWithFormat:@"%@  •  %@", node.delay, node.desc];
    self.speedDotView.backgroundColor = [UIColor redColor];
    self.selectedIndicator.hidden = !node.isSelected;
    NSInteger delay = [[node.delay componentsSeparatedByString:@"ms"].firstObject integerValue];
    if (delay < 300){
        self.speedDotView.backgroundColor = [UIColor colorWithRed:46/255.0 green:204/255.0 blue:113/255.0 alpha:1.0];
    }else if (delay < 600){
        self.speedDotView.backgroundColor = [UIColor colorWithRed:241/255.0 green:196/255.0 blue:15/255.0 alpha:1.0];
    }else{
        self.speedDotView.backgroundColor = [UIColor colorWithRed:231/255.0 green:76/255.0 blue:60/255.0 alpha:1.0];
    }
    // 处理不可用状态
    if (!node.isAvailable) {
        self.nameLabel.alpha = 0.5;
        self.detailLabel.alpha = 0.5;
        self.speedDotView.alpha = 0.5;
        self.cardView.alpha = 0.5;
    } else {
        self.nameLabel.alpha = 1.0;
        self.detailLabel.alpha = 1.0;
        self.speedDotView.alpha = 1.0;
        self.cardView.alpha = 1.0;
    }
}
- (void)setIsCurrent:(BOOL)isCurrent{
    _isCurrent = isCurrent;
    if (isCurrent){
        self.currentLab.text = @"当前线路";
    }else{
        self.currentLab.text = @"";
    }
}
- (void)animateSelection {
    // 添加选中动画效果
    self.cardView.transform = CGAffineTransformMakeScale(0.95, 0.95);
    [UIView animateWithDuration:0.2 animations:^{
        self.cardView.transform = CGAffineTransformIdentity;
    }];
}

@end

